/*il file di test serve per testare l'algoritmo per la generazione della costruttiva dinamica
 *utilizzare questo test sul file grafo dynamicConstructive.dat
 *con un unico camion
 */


#include <stdlib.h>
#include <stdio.h>
#include "ltm.h"
#include "parser.h"

int main(int argn, const char** args){

	FILE* fgraph;
	int N;
	int K;


	PedgeID _12=initEdgeID(0,1);
	validateEdgeID(_12);
	PedgeID _13=initEdgeID(0,2);
	validateEdgeID(_13);
	PedgeID _14=initEdgeID(0,3);
	validateEdgeID(_14);
	PedgeID _15=initEdgeID(0,4);
	validateEdgeID(_15);
	PedgeID _16=initEdgeID(0,5);
	validateEdgeID(_16);
	PedgeID _23=initEdgeID(1,2);
	validateEdgeID(_23);
	PedgeID _24=initEdgeID(1,3);
	validateEdgeID(_24);
	PedgeID _25=initEdgeID(1,4);
	validateEdgeID(_25);
	PedgeID _26=initEdgeID(1,5);
	validateEdgeID(_26);
	PedgeID _34=initEdgeID(2,3);
	validateEdgeID(_34);
	PedgeID _35=initEdgeID(2,4);
	validateEdgeID(_35);
	PedgeID _36=initEdgeID(2,5);
	validateEdgeID(_36);
	PedgeID _45=initEdgeID(3,4);
	validateEdgeID(_45);
	PedgeID _46=initEdgeID(3,5);
	validateEdgeID(_46);
	PedgeID _56=initEdgeID(4,5);
	validateEdgeID(_56);



	Pgraph pgraph;
	Psolution constructivesolution;
	PtabooState ptaboostate;
	PLTMFrequencies matrixes;
	if (argn!=3){
		fprintf(stderr,"numero di parametri errati.\n 1)path del grafo\n2)numero camion\n");
		return EXIT_FAILURE;
	}
	fgraph=fopen(args[1],"r");
	K=1;//atoi(args[2]);

	pgraph=parseGraph(fgraph,&N);
	graphCorrector(pgraph);
	dijkstraFiller(pgraph);
	initSmartWeights(pgraph);
	constructivesolution=initSolution(&K,&N);
	constructivesolution->states[0]=initVehicleState();
	ptaboostate=initTabooState(&N,&K,0);

	printf("inizializzo LTMFrequencies...\n");
	matrixes=initLTMFrequencies(&K,pgraph->N);
	//printf("stamp la matrice vuota...\n");
	//printLTMFrequencies(stdout,matrixes);
	//creiamo la matrice fittizia
	printf("imposto manualmente matrixes...\n");
	setFMatrixValue(_12,matrixes->vehicleMatrices[0],0);
	setFMatrixValue(_13,matrixes->vehicleMatrices[0],0);
	setFMatrixValue(_14,matrixes->vehicleMatrices[0],2);
	setFMatrixValue(_15,matrixes->vehicleMatrices[0],2);
	setFMatrixValue(_16,matrixes->vehicleMatrices[0],0);

	setFMatrixValue(_23,matrixes->vehicleMatrices[0],0);
	setFMatrixValue(_24,matrixes->vehicleMatrices[0],0);
	setFMatrixValue(_25,matrixes->vehicleMatrices[0],2);
	setFMatrixValue(_26,matrixes->vehicleMatrices[0],1);

	setFMatrixValue(_34,matrixes->vehicleMatrices[0],0);
	setFMatrixValue(_35,matrixes->vehicleMatrices[0],0);
	setFMatrixValue(_36,matrixes->vehicleMatrices[0],3);

	setFMatrixValue(_45,matrixes->vehicleMatrices[0],0);
	setFMatrixValue(_46,matrixes->vehicleMatrices[0],1);

	setFMatrixValue(_56,matrixes->vehicleMatrices[0],1);

	printFMatrix(stdout,matrixes->vehicleMatrices[0]);

	buildDynamicConstructiveWithLowFrequencyEdges(
		pgraph,
		matrixes,
		constructivesolution,
		ptaboostate,
		5
	);

	printSolution(stdout,constructivesolution);
	int i;
	for (i=0;i<*ptaboostate->K;i++){
		printTabooList(stdout,ptaboostate->tls[i]);
	}
	drawSolutionFromScratchDefaults(
		"dynamicConstructiveTest.bmp",
		constructivesolution,
		pgraph->depot
	);


	printFMatrix(stdout,matrixes->vehicleMatrices[0]);
	int j;
	EdgeID e;
	printf("controllo ora che il campo minimum di fMatrix funzioni a dovere...\n");
	for (i=0;i<*matrixes->vehicleMatrices[0]->N;i++){
		e.i=i;
		for (j=(i+1);j<*matrixes->vehicleMatrices[0]->N;j++){
			e.j=j;
			addFMatrixValue(&e,matrixes->vehicleMatrices[0],1);
		}
	}
	printFMatrix(stdout,matrixes->vehicleMatrices[0]);
	printf("chi e' il minimo??? %d\n",matrixes->vehicleMatrices[0]->minimum);
	printf("quanti ce ne sono??? %d\n",matrixes->vehicleMatrices[0]->minimumNumber);

	printf("dealloco...\n");
	//verifico che i free funzionino tutti correttamente
	freeLTMFrequencies(matrixes);
	printf("deallocato!\n");
	printf("puntatore a matrixes %p\n",matrixes);
	//questo dovrebbe dare NIL
	printf("%p\n",matrixes->vehicleMatrices[0]);
	printf("FINE.\n");
	return EXIT_SUCCESS;
}


